package util

import "math/rand"

// 是否为大写英文字母
func IsASCIIUpper(c byte) bool {
	return 'A'<=c && c<='Z'
}

// 大小写英文字母相互转换
func UpperLowerExchange(c byte) byte {
	return c ^ ' '
}

func Camel2Snake(s string) string {
	if len(s) == 0 {
		return ""
	}
	t:=make([]byte,0,len(s)+4)
	if IsASCIIUpper(s[0]) {
		t = append(t, UpperLowerExchange(s[0]))
	} else {
		t = append(t, s[0])
	}
	for i:=1;i<len(s);i++{
		c:=s[i]
		if IsASCIIUpper(c) {
			t = append(t,'_', UpperLowerExchange(c))
		} else {
			t = append(t, c)
		}
	}
	return string(t)
}

var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")

func RandStringRunes(n int) string {
	b:=make([]rune, n)
	L:=len(letterRunes)
	for i:=range b {
		b[i]= letterRunes[rand.Intn(L)]
	}
	return string(b)
}
